import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class OplatformServerService extends BaseService<any> {
  private readonly logger = new Logger(OplatformServerService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * server
   * 对应 PHP: OplatformServerService_admin::server()
   * 逻辑类型: undefined - undefined
   */
  async server() {
    // 基于PHP真实逻辑: server
    // PHP原文: $app = CoreOplatformService::app();         $server = $app->getServer();          // 授权取消         $server->handleUnauthorized(function($message, \Clos...
const app = CoreOplatformService.app(];
        const server = app.getServer();

        // 授权取消
        server.handleUnauthorized(function(message, \Closure next) {
            const authorizer_appid = message.AuthorizerAppid;

            const site_id = CoreOplatformService.getSiteIdByAuthorizerAppid(authorizer_appid);
            request().siteId(site_id];

            this.oplatformService.clearAuthorization(authorizer_appid];
            return next(message);
  }

  /**
   * message
   * 对应 PHP: OplatformServerService_admin::message()
   * 逻辑类型: undefined - undefined
   */
  async message(appid: any) {
    // 基于PHP真实逻辑: message
    // PHP原文: $app = CoreOplatformService::app();         $server = $app->getServer();          $site_id = CoreOplatformService::getSiteIdByAuthorizerAppid($appid);...
const app = CoreOplatformService.app(];
        const server = app.getServer(];

        const site_id = CoreOplatformService.getSiteIdByAuthorizerAppid(appid);

        server.with(function(message, \Closure next) {
            switch (message.Event) {
                case 'weapp_audit_success':
                    this.weappAuditSuccess(message];
                    break;
                case 'weapp_audit_fail':
                    this.weappAuditFail(message];
                    break;
            }
            return next(message);
  }

  /**
   * weappAuditSuccess
   * 对应 PHP: OplatformServerService_admin::weappAuditSuccess()
   * 逻辑类型: undefined - undefined
   */
  async weappAuditSuccess(message: any) {
    // 基于PHP真实逻辑: weappAuditSuccess
    // PHP原文: $site_id = CoreOplatformService::getSiteIdByAuthorizerAppid($message['ToUserName']);         CoreOplatformService::releaseWeapp($site_id);         (ne...
const site_id = CoreOplatformService.getSiteIdByAuthorizerAppid(message.ToUserName];
        CoreOplatformService.releaseWeapp(site_id);
        this.weappVersionService.where([site_id: site_id, status: CloudDict.APPLET_AUDITING )).update([status: CloudDict.APPLET_PUBLISHED )];

        // 发布后重新设置下域名
        request().siteId(site_id];
        this.weappVersionService.setDomain();
        return true;
    }

    /**
     * 小程序审核未通过
     * @param message
     * @return true
     */
    private async weappAuditFail(message) {
        const site_id = CoreOplatformService.getSiteIdByAuthorizerAppid(message.ToUserName);
        this.weappVersionService.where([site_id: site_id, status: CloudDict.APPLET_AUDITING )).update([status: CloudDict.APPLET_AUDIT_FAIL, fail_reason: message.Reason )];
        return true;
    }
}
  }

  /**
   * weappAuditFail
   * 对应 PHP: OplatformServerService_admin::weappAuditFail()
   * 逻辑类型: undefined - undefined
   */
  async weappAuditFail(message: any) {
    // 基于PHP真实逻辑: weappAuditFail
    // PHP原文: $site_id = CoreOplatformService::getSiteIdByAuthorizerAppid($message['ToUserName']);         (new WeappVersion())->where(['site_id' => $site_id, 'stat...
const site_id = CoreOplatformService.getSiteIdByAuthorizerAppid(message.ToUserName);
        this.weappVersionService.where([site_id: site_id, status: CloudDict.APPLET_AUDITING )).update([status: CloudDict.APPLET_AUDIT_FAIL, fail_reason: message.Reason )];
        return true;
    }
}
  }
}
